package com.lovi.net_.tcp;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 字符流方式
 */
public class SocketTCPServer03 {
    public static void main(String[] args) throws IOException {
        //监听端口
        ServerSocket serverSocket = new ServerSocket(9999);
        //程序会阻塞在这，当9999端口被连接的时候，我们拿到连接9999的socket对象
        System.out.println("服务器端已经启动，等待连接...");
        Socket accept = serverSocket.accept();
        //服务器端读取客户发送过来的东西
        InputStream inputStream = accept.getInputStream();
        //接受字符流
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        System.out.println(reader.readLine());

        //读取到之后发消息给客户端
        OutputStream outputStream = accept.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));
        writer.write("服务端：你好呀客户端！");
        writer.newLine();
        writer.flush();//不刷新写不进去

        writer.close();
        outputStream.close();
        inputStream.close();
        accept.close();
        System.out.println("服务器端关闭！");
    }
}
